package cn.sdormitory.controller.smartdor;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.smartdor.entity.StudentTrajectory;
import cn.sdormitory.smartdor.entity.StudentTrajectoryExcel;
import cn.sdormitory.smartdor.entity.StudentTrajectoryPageParam;
import cn.sdormitory.smartdor.service.StudentTrajectoryService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 *
 * @author alex
 * @date 2024/9/26 11:24
 * @return
 */
@RestController
@Api(tags = "StudentTrajectory => 学员信息管理")
@RequestMapping("/student/trajectory")
public class StudentTrajectoryController {

    @Resource
    StudentTrajectoryService trajectoryService;

    @ApiOperation("获取学生行动轨迹")
    @GetMapping("/getStuTrajectory/{studentId}")
    public CommonResult getStuTrajectory(@PathVariable("studentId") Long studentId){
        return CommonResult.success(trajectoryService.getStuTrajectory(studentId));
    }

    @ApiOperation("分页查询学生行动轨迹")
    @PostMapping("/findPageList")
    public CommonResult findPageList(@RequestBody StudentTrajectoryPageParam query){
        IPage<StudentTrajectory> pageList = trajectoryService.findPageList(query);
        return CommonResult.success(CommonPage.restPage(pageList));
    }

    @ApiOperation("班主任分页查询学生行动轨迹")
    @PostMapping("/teacherFindPageList")
    public CommonResult teacherFindPageList(@RequestBody StudentTrajectoryPageParam query){
        IPage<StudentTrajectory> pageList = trajectoryService.teacherFindPageList(query);
        return CommonResult.success(CommonPage.restPage(pageList));
    }

    @ApiOperation("导出学生行动轨迹")
    @PostMapping("/exportTrajectory")
    public void exportTrajectory(HttpServletResponse response, @RequestBody StudentTrajectoryPageParam query) throws IOException {
        List<StudentTrajectoryExcel> trajectoryList = trajectoryService.exportTrajectory(query);
        EasyExcel.write(response.getOutputStream(), StudentTrajectoryExcel.class)
                .sheet( "闸机出入记录")
                .doWrite(trajectoryList);
    }
}
